home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / iPod / iPodderX.sit / iPodderX / iPodderX.app / Contents / Resources / zurllib.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2005-01-07  |  5.2 KB  |  133 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. from urllib import *
  5. from urllib2 import *
  6. from gzip import GzipFile
  7. from StringIO import StringIO
  8. from __init__ import version
  9. import pprint
  10. DEBUG = 0
  11.  
  12. class HTTPContentEncodingHandler(HTTPHandler):
  13.     '''Inherit and add gzip/deflate/etc support to HTTP gets.'''
  14.     
  15.     def http_open(self, req):
  16.         req.add_header('Accept-Encoding', 'gzip')
  17.         req.add_header('User-Agent', 'BitTorrent/' + version)
  18.         if DEBUG:
  19.             print 'Sending:'
  20.             print req.headers
  21.             print '\n'
  22.         
  23.         fp = HTTPHandler.http_open(self, req)
  24.         headers = fp.headers
  25.         if DEBUG:
  26.             pprint.pprint(headers.dict)
  27.         
  28.         url = fp.url
  29.         return addinfourldecompress(fp, headers, url)
  30.  
  31.  
  32.  
  33. class addinfourldecompress(addinfourl):
  34.     '''Do gzip decompression if necessary. Do addinfourl stuff too.'''
  35.     
  36.     def __init__(self, fp, headers, url):
  37.         if headers.has_key('content-encoding') and headers['content-encoding'] == 'gzip':
  38.             if DEBUG:
  39.                 print 'Contents of Content-encoding: ' + headers['Content-encoding'] + '\n'
  40.             
  41.             self.gzip = 1
  42.             self.rawfp = fp
  43.             fp = GzipStream(fp)
  44.         else:
  45.             self.gzip = 0
  46.         return addinfourl.__init__(self, fp, headers, url)
  47.  
  48.     
  49.     def close(self):
  50.         self.fp.close()
  51.         if self.gzip:
  52.             self.rawfp.close()
  53.         
  54.  
  55.     
  56.     def iscompressed(self):
  57.         return self.gzip
  58.  
  59.  
  60.  
  61. class GzipStream(StringIO):
  62.     """Magically decompress a file object.
  63.  
  64.        This is not the most efficient way to do this but GzipFile() wants
  65.        to seek, etc, which won't work for a stream such as that from a socket.
  66.        So we copy the whole shebang info a StringIO object, decompress that
  67.        then let people access the decompressed output as a StringIO object.
  68.  
  69.        The disadvantage is memory use and the advantage is random access.
  70.  
  71.        Will mess with fixing this later.
  72.     """
  73.     
  74.     def __init__(self, fp):
  75.         self.fp = fp
  76.         compressed = StringIO()
  77.         r = fp.read()
  78.         while r:
  79.             compressed.write(r)
  80.             r = fp.read()
  81.         compressed.seek(0, 0)
  82.         gz = GzipFile(fileobj = compressed)
  83.         str = ''
  84.         r = gz.read()
  85.         while r:
  86.             str += r
  87.             r = gz.read()
  88.         compressed.close()
  89.         gz.close()
  90.         StringIO.__init__(self, str)
  91.         del str
  92.  
  93.     
  94.     def close(self):
  95.         self.fp.close()
  96.         return StringIO.close(self)
  97.  
  98.  
  99.  
  100. def test():
  101.     '''Test this module.
  102.  
  103.        At the moment this is lame.
  104.     '''
  105.     print 'Running unit tests.\n'
  106.     
  107.     def printcomp(fp):
  108.         
  109.         try:
  110.             if fp.iscompressed():
  111.                 print 'GET was compressed.\n'
  112.             else:
  113.                 print 'GET was uncompressed.\n'
  114.         except:
  115.             print "no iscompressed function!  this shouldn't happen"
  116.  
  117.  
  118.     print 'Trying to GET a compressed document...\n'
  119.     fp = urlopen('http://a.scarywater.net/hng/index.shtml')
  120.     print fp.read()
  121.     printcomp(fp)
  122.     fp.close()
  123.     print 'Trying to GET an unknown document...\n'
  124.     fp = urlopen('http://www.otaku.org/')
  125.     print fp.read()
  126.     printcomp(fp)
  127.     fp.close()
  128.  
  129. install_opener(build_opener(HTTPContentEncodingHandler))
  130. if __name__ == '__main__':
  131.     test()
  132.  
  133.